import count from '@dfeidao/atom-nodejs/db/count';
import operate from '@dfeidao/atom-nodejs/db/operate';
// import log from '@dfeidao/atom-nodejs/logger/log';
import { systemid } from '../../atom/config';
interface Message {
	productid: string;
	level_name: string;
	level_no: string;
	usercode: string;
	username: string;
}

export default async function atom(msg: Message, action_id: string, session_id: string) {
	const { prepare, exec } = count(action_id, session_id, '12');
	const { update, exec: e } = operate(action_id, session_id, '12');
	const { insert: i, exec: x } = operate(action_id, session_id, systemid);
	prepare('tenant_level', { productid: msg.productid, level_name: msg.level_name, level_no: { $nin: [msg.level_no] } });
	const [cou] = await exec();
	if (cou === 0) {
		update('tenant_level', { productid: msg.productid, level_no: msg.level_no }, { level_name: msg.level_name });
		i('operation_log', {
			pub_user_no: msg.usercode,
			resume_name: msg.username,
			operate_type: '修改',
			op_cont: `“${msg.username}”在[等级维护]页面“修改”了一条编号为“${msg.level_no}”的数据`, productid: msg.productid
		});
		const ret = await e();
		await x();
		if (ret.code === 1) {
			return 'success';
		} else {
			return 'fail';
		}
	} else {
		return 'toomuch';
	}
}
